<?php

namespace App\Http\Controllers;

use App\Theme;
use App\User;
use App\Writing;
use App\WritingComment;
use App\WritingLike;
use App\Writings;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function index(Request $request)
    {

        return view('web.user.index');

    }

    //上传作品
    public function upload_writing()
    {
        return view('web.user.uploadWriting');
    }

    //得到主题
    public function get_theme(Request $request)
    {
        $category_id = $request->category_id;
        $theme = Theme::where('category_id', $category_id)->get();
        return view('web.user.get_theme', compact('theme'));

    }

    //提交作品
    public function writing_store(Request $request)
    {

        $rules = [
            'name' => 'required',
            'desc' => 'required',
            'category_id' => 'required',
            'theme_id' => 'required',
        ];
        $messages = [

            'theme_id.required' => trans('web.select_theme'),
            'name.required' => trans('web.input_title'),

        ];


        if ($request->video_url) {

            /* $rules['video_url']='ipv4';*/
            //  $request->video_url = str_replace('watch?v=', 'embed/', $request->video_url);
            //获取url
            if (strstr($request->video_url, 'youtube')) {
                $query_url = parse_url($request->video_url);
                if ($query_url && isset($query_url['query'])) {
                    parse_str($query_url['query']);

                    if (isset($v)) {
                        $video_url = 'https://www.youtube.com/embed/' . $v;
                    }
                }elseif(strstr($request->video_url,'www.youtube.com/embed/')){
                    $video_url=$request->video_url;
                } else
                {
                    return redirect()->back()->withInput()->withErrors(
                        ['video_url' => trans('web.your_video')]
                    );
                }
            }elseif (strstr($request->video_url,'youtu.be')){
                $video_url=str_replace('https://youtu.be/','https://www.youtube.com/embed/',$request->video_url);
            }elseif(strstr($request->video_url,'www.youtube.com/embed/')){
                $video_url=$request->video_url;
            } else {
                $video_url = '';
            }
        }
        if(!isset($video_url)){
            $video_url='';
        }
        $this->validate($request, $rules, $messages);
        $writing = new Writing();
        $writing->name = $request->name;
        $writing->desc = $request->desc;
        $writing->category_id = $request->category_id;
        $writing->theme_id = $request->theme_id;
        $writing->thumb_img = $request->thumb_img ? $request->thumb_img : $request->master1;
        $writing->master = [$request->master1, $request->master2, $request->master3];
        $writing->video_url = $video_url;
        $writing->video = $request->video;
        $writing->author_id = Auth::user()->id;
        $writing->save();
        return redirect()->back()->with(
            ['status' => '上传成功']
        );
    }

    //作品列表
    public function writing_list()
    {
        return view('web.user.writing_list');
    }
    //别人的作品列表
    public function his_detail(Request $request,$id){
        $user= User::find($id);
        return view('web.his.writing_list',compact('user'));
    }
    //得到更多作品
    public function get_more_writing(Request $request)
    {
        $id=$request->id;
        $config = config('admin.writing');
        $list = Writing::with(['theme', 'user', 'like'])
            ->where("author_id", $id)
            ->where('is_display', 1)
            ->orderBy("id", "desc")
            ->paginate(12);

        return view('web.user.get_more_writing', compact('list', 'config'));
    }

    //作品详情
    public function writing_detail(Request $request)
    {
        $id = $request->id;
        $detail = Writing::with(['theme', 'like' => function ($query) {
            if (Auth::check()) {
                return $query->where('user_id', Auth::id());
            } else {
                return $query->where('user_id', null);
            }
        }])->where('is_display', 1)->findOrFail($id);
        if (!$detail) {
            abort(404);
        }
        return view('web.user.writing_detail', compact('detail'));
    }

    //删除作品
    public function writing_del(Request $request)
    {
        $writing_id = $request->writing_id;
        $writing = Writing::find($writing_id);
        if (!Auth::user()->can('writing_del') && !Auth::user()->can('writing_del1', $writing)) {
            abort(404);
        }
        //删除评论
        WritingComment::where('writing_id', $writing_id)->delete();
        //删除喜欢的
        WritingLike::where('writing_id', $writing_id)->delete();

        $writing->delete();
        return redirect()->route('index')->with(
            ['status' => trans('web.writing_del_success')]
        );
    }

    //编辑作品
    public function writing_edit(Request $request)
    {
        $writing_id = $request->writing_id;
        $writing = Writings::findOrFail($writing_id);
        if (!Auth::user()->can('writing_del') && !Auth::user()->can('writing_del1', $writing)) {
            abort(404);
        }
        $theme = Theme::where('category_id', $writing->category_id)->get();
        return view('web.user.uploadWriting', compact('writing', 'theme'));
    }

    public function writing_update(Request $request, $id)
    {
        $writing = Writing::findOrFail($id);
        if (!Auth::user()->can('writing_del') && !Auth::user()->can('writing_del1', $writing)) {
            abort(404);
        }
        $rules = [
            'name' => 'required',
            'desc' => 'required',
            'category_id' => 'required',
            'theme_id' => 'required',

        ];
        $messages = [
            'theme_id.required' => trans('web.select_theme'),
            'name.required' => trans('web.input_title'),
        ];
        if ($request->video_url) {

            /* $rules['video_url']='ipv4';*/
            //  $request->video_url = str_replace('watch?v=', 'embed/', $request->video_url);
            //获取url
            if (strstr($request->video_url, 'youtube')) {

                $query_url = parse_url($request->video_url);
                if ($query_url && isset($query_url['query'])) {
                    parse_str($query_url['query']);

                    if (isset($v)) {
                        $video_url = 'https://www.youtube.com/embed/' . $v;
                    }
                }elseif(strstr($request->video_url,'www.youtube.com/embed/')){
                    $video_url=$request->video_url;
                } else
                {
                    return redirect()->back()->withInput()->withErrors(
                        ['video_url' => trans('web.your_video')]
                    );
                }
            }elseif (strstr($request->video_url,'youtu.be')){
                $video_url=str_replace('https://youtu.be/','https://www.youtube.com/embed/',$request->video_url);
            }elseif(strstr($request->video_url,'www.youtube.com/embed/')){
                $video_url=$request->video_url;
            } else {
                $video_url = '';
            }
        }

        if(!isset($video_url)){
            $video_url='';
        }
        $this->validate($request, $rules, $messages);

        $writing->name = $request->name;
        $writing->desc = $request->desc;
        $writing->category_id = $request->category_id;
        $writing->theme_id = $request->theme_id;
        $writing->thumb_img = $request->thumb_img ? $request->thumb_img : $request->master1;
        $writing->master = [$request->master1, $request->master2, $request->master3];
        $writing->video_url = $video_url;
        $writing->video = $request->video;
        $writing->author_id = Auth::user()->id;
        $writing->save();
        return redirect()->back()->with(
            ['status' => trans("web.writing_update_success")]);
    }

    public function resetPassword()
    {
        $user = Auth::user();
        return view('web.user.resetPassword', compact('user'));
    }

    public function resetPasswordPost(Request $request)
    {
        $rules = [
            'name' => 'required|string|max:255',
        ];
        $message = [];
        if ($request->password) {
            $rules['password'] = 'required|string|min:6|confirmed';
        }
        $this->validate($request, $rules, $message);
        $user = User::find(Auth::id());

        $user->name = $request->name;
        if ($request->password) {
            $user->password = bcrypt($request->password);
        }
        $user->save();
        return redirect()->back()->with(['status' => trans("web.update_setting_success")]);
    }


}
